﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEDangerCategoryHSEService
{
    private readonly SimpleDbContext _context;
    public QHSEDangerCategoryHSEService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEDangerCategoryHSEModel>> GetAsync()
    {
        var violationHSEs = await _context.Set<QHSESysDangerCategoryHSE>().ToListAsync();
        return MapperHelper.Map<List<QHSEDangerCategoryHSEModel>>(violationHSEs);
    }
    public async Task<QHSEDangerCategoryHSEPageResultModel> GetResult(QHSEDangerCategoryHSEPageInputModel inputModel)
    {
        var dangerCategory = await _context.Set<QHSESysDangerCategory>().FindAsync(inputModel.Id);
        var hses = new List<QHSESysViolationHSE>();
        if (dangerCategory.Item == "其它" || dangerCategory.Item == "其他")
        {
            hses =await _context.Set<QHSESysViolationHSE>().ToListAsync();
        }
        else
        {
            hses = await _context.Set<QHSESysDangerCategoryHSE>()
                            .AsNoTracking()
                         .Join(_context.Set<QHSESysViolationHSE>(), vcHse => vcHse.HSECode, Hse => Hse.Code, (vcHse, Hse) => new { vcHse, Hse })
                         .Where(u => u.vcHse.Code == dangerCategory.Code)
                         .Select(x => x.Hse).ToListAsync();
        }
       var hseModels = MapperHelper.Map<List<QHSEViolationHSEModel>>(hses);
        var resultModel = new QHSEDangerCategoryHSEPageResultModel
        {
            Result = hseModels
        };
        return resultModel;
    }
}
